package dingyunchi.job.controller;

import dingyunchi.job.entity.Score;
import dingyunchi.job.service.ScoreService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/mybatis/scores")
public class ScoreController {
    private final ScoreService scoreService;

    public ScoreController(ScoreService scoreService) {
        this.scoreService = scoreService;
    }

    @PostMapping
    public String save(@RequestBody Score score) {
        try {
            Score savedScore = scoreService.save(score);
            return "成绩添加成功，id为：" + savedScore.getId();
        } catch (Exception e) {
            return "成绩添加失败，原因：" + e.getMessage();
        }
    }

    @GetMapping("/{id}")
    public Score findById(@PathVariable Long id) {
        return scoreService.findById(id);
    }

    @GetMapping
    public List<Score> findAll() {
        return scoreService.findAll();
    }

    @PutMapping
    public String update(@RequestBody Score score) {
        try {
            scoreService.update(score);
            return "成绩信息修改成功";
        } catch (Exception e) {
            return "成绩信息修改失败，原因：" + e.getMessage();
        }
    }

    @DeleteMapping("/{id}")
    public String delete(@PathVariable Long id) {
        try {
            scoreService.delete(id);
            return "成绩删除成功";
        } catch (Exception e) {
            return "成绩删除失败，原因：" + e.getMessage();
        }
    }

    @GetMapping("/ranking")
    public List<Score> getScoreRanking() {
        return scoreService.getScoreRanking();
    }
}